package sz.szsmk.citizencard.ui.adapter;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserOrderViewBinding;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/23 19:20
 * description:
 */
public class UserOrderAdapter extends DelegateAdapter.Adapter<UserOrderAdapter.UserOrderHolder> {

    private FragmentActivity mContext;
    private LayoutHelper mDefaultLayoutHelper;
    private String orderExtent;
    private UserOrderItemAdapter itemAdapter;

    public UserOrderAdapter(FragmentActivity activity, DefaultLayoutHelper defaultLayoutHelper) {
        mContext = activity;
        mDefaultLayoutHelper = defaultLayoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.user_order_view;
    }

    @Override
    public UserOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return UserOrderHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserOrderHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, UserOrderViewBinding binding) {
        RecyclerView userOrderRv = binding.userOrderRv;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 5);
        userOrderRv.setLayoutManager(gridLayoutManager);
        itemAdapter = new UserOrderItemAdapter(mContext, orderExtent);
        userOrderRv.setAdapter(itemAdapter);
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setOrderExtent(String orderExtent) {
        this.orderExtent = orderExtent;
    }

    public static class UserOrderHolder extends RecyclerView.ViewHolder {

        private UserOrderViewBinding binding;

        public static UserOrderHolder creat(ViewGroup parent) {
            UserOrderViewBinding binding = UserOrderViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserOrderHolder(binding);
        }

        public UserOrderHolder(UserOrderViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }
}
